package com.example.a2209_mianshi.ui.mall.viewModel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.a2209_mianshi.base.BaseViewModel
import com.example.a2209_mianshi.base.state.Default
import com.example.a2209_mianshi.base.state.Empty
import com.example.a2209_mianshi.base.state.Failed
import com.example.a2209_mianshi.base.state.Loading
import com.example.a2209_mianshi.base.state.RequestEvent
import com.example.a2209_mianshi.base.state.Success
import com.example.a2209_mianshi.ui.mall.model.MallBannerModel
import com.example.a2209_mianshi.ui.mall.model.MallGoodModel
import com.example.a2209_mianshi.ui.mall.model.MallGroupModel
import com.example.a2209_mianshi.ui.mall.repository.MallRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject
const val TAG = "MallViewModel"
@HiltViewModel
class MallViewModel @Inject constructor(private val mallRepository: MallRepository):BaseViewModel() {
    private val _mallMallGroupStateFlow by lazy {
        MutableStateFlow<RequestEvent>(Default)
    }
    val mallMallGroupStateFlow = _mallMallGroupStateFlow.asStateFlow()
    private val _mallMallGroupLiveData by lazy {
        MutableLiveData<MallGoodModel>()
    }
    val mallAdInfoLiveData: LiveData<MallGoodModel> = _mallMallGroupLiveData
    private val _bannerMallInfoStateFlow by lazy {
        MutableStateFlow<RequestEvent>(Default)
    }
    val bannerMallInfoStateFlow = _bannerMallInfoStateFlow.asStateFlow()
    private val _bannerMallInfoLiveData by lazy {
        MutableLiveData<MallBannerModel>()
    }
    val bannerMallInfoLiveData: LiveData<MallBannerModel> = _bannerMallInfoLiveData
    fun cmallAdInfo(){
        execute(
            action = mallRepository.cmallMallGroup(),
            onStart = {
                //开始请求
                _mallMallGroupStateFlow.value = Loading
                Log.d(TAG, "cmallAdInfo: MallViewModel开始请求数据")
            },
            onError = {
                //请求失败
                _mallMallGroupStateFlow.value = Failed(it)
                Log.d(TAG, "cmallAdInfo: MallViewModel请求数据失败"+it.toString())
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _mallMallGroupStateFlow.value = Empty
                    Log.d(TAG, "cmallAdInfo: MallViewModel请求数据到的数据为空")
                }else{
                    _mallMallGroupStateFlow.value = Success

                    _mallMallGroupLiveData.value = it.data
                    Log.d(TAG, "cmallAdInfo: MallViewModel请求数据成功")
                }

            }
        )
    }
    fun cmallBannerInfo(){
        execute(
            action = mallRepository.cmallBannerInfo(),
            onStart = {
                //开始请求
                _bannerMallInfoStateFlow.value = Loading
                Log.d(TAG, "cmallBannerInfo: MallViewModel开始请求数据")
            },
            onError = {
                //请求失败
                _bannerMallInfoStateFlow.value = Failed(it)
                Log.d(TAG, "cmallBannerInfo: MallViewModel请求数据失败"+it.toString())
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _bannerMallInfoStateFlow.value = Empty
                    Log.d(TAG, "cmallBannerInfo: MallViewModel请求数据到的数据为空")
                }else{
                    _bannerMallInfoStateFlow.value = Success
                    _bannerMallInfoLiveData.value = it.data
                    Log.d(TAG, "cmallBannerInfo: MallViewModel请求数据成功")
                }
            }
        )
    }
}